<template>
    <div class="login-wrap">
        <div class="ms-title">后台管理系统</div>
        <div class="ms-login">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="0px" v-loading="login_actions.disabled" :element-loading-text="'正在登录...'" >
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" :autofocus="true" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="密码" v-model="loginForm.password" @keyup.enter.native="submitForm('loginForm')"></el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data: function(){
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                login_actions: {
                    disabled: false
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            submitForm(formName) {
                var self = this;
                self.$refs[formName].validate((valid) => {
                    if (valid) {
                        self.login_actions.disabled = true
                        localStorage.setItem('ms_username',self.loginForm.username);

                       
                        //console.log(this.$router.options.routes)
                        
                        //console.log(self.loginForm)
                        self.$$api_permission_Login(self.loginForm, data =>{
                           
                                var userInfo = {
                                    userName:self.loginForm.username,
                                    token:data.Token,
                                    web_routes:['/permission/home','/permission/admin/list','/permission/application/list','/trade/home','/trade/order','/trade/order/list'],
                                    api_routes:{},
                                    ui_keys:{}
                                }

                                this.$store.dispatch("set_userInfo",userInfo)

                                self.$router.push('/permission');
                            
                            
                        },{
                            errFn: () =>{
                                self.login_actions.disabled = false;
                            },
                            nonCarryToken: true //不携带token
                        });
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .login-wrap{
        position: relative;
        width:100%;
        height:100%;
    }
    .ms-title{
        position: absolute;
        top:50%;
        width:100%;
        margin-top: -230px;
        text-align: center;
        font-size:30px;
        color: #fff;

    }
    .ms-login{
        position: absolute;
        left:50%;
        top:50%;
        width:300px;
        height:160px;
        margin:-150px 0 0 -190px;
        padding:40px;
        border-radius: 5px;
        background: #fff;
    }
    .login-btn{
        text-align: center;
    }
    .login-btn button{
        width:100%;
        height:36px;
    }
</style>
